/*
 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
 */

#include <stdio.h>
#include <stdlib.h>

#include "aos/kernel.h"
#include "aos/hal/rtc.h"
#include "app_entry.h"

/********************************************************************************
n        -- rtc test times
delay_ms -- delay time between tests in ms
**********************************************************************************/
int rtc_test(int n,  int delay_ms)
{
    int ret = 0;
    volatile int count = n;
    rtc_dev_t rtc;
    rtc_time_t time;

    printf("enter rtc test\r\n");

    ret = hal_rtc_init(&rtc);
    if (ret != 0) {
        printf("hal_rtc_init fail 0x%08x\r\n", ret);
        return -1;
    }


    time.date = 12;
    time.month = 11;
    time.year = 19;
    time.hr = 20;
    time.min = 0;
    time.sec = 0;
    time.weekday = 2;

    ret = hal_rtc_set_time(&rtc, &time);
    if (ret != 0) {
        printf("hal_rtc_set_time fail 0x%08x\r\n", ret);
        return -1;
    }

    printf("\n\ryear-month-day  hour-min-sec  weekday\n\r");

    while(count--) {
        memset(&time, 0 ,sizeof(time));
        ret = hal_rtc_get_time(&rtc, &time);
        if (ret != 0) {
            printf("hal_rtc_get_time fail 0x%08x\r\n", ret);
            return -1;
        }
        printf("%02d-%02d-%02d  %02d-%02d-%02d  %d\n\r",time.year, time.month, time.date, time.hr, time.min ,time.sec, time.weekday);
        aos_msleep(delay_ms);
    };

    ret = hal_rtc_finalize(&rtc);
    if (ret != 0) {
        printf("hal_rtc_finalize fail 0x%08x\r\n", ret);
        return -1;
    }

    return 0;
}
